# -*- coding: iso-8859-2 -*-

import os
import re

from authorshipanalyzer.file import Path

class ResourceReader:
    '''Read property files'''
    
    def readKeyValueFile(self, filename):
        '''
        Function which reads list of key value
        from a file given by a @param filename.
        Pairs are separated by a new line separator
        Key and value are separated by a space character. 
        '''
        result = {}
        
        file = open(filename, "r")
        for line in file.readlines():
            line = line.strip()
            if line != "":
                line.lower()
                items = line.split()
                assert len(items) == 2
                result[items[0]] = items[1];
        file.close()
        
        return result
    
    def readWordList(self, filename):
        '''
        Function which reads list of words
        from a file given by a @param filename.
        Files are separated by a new line separator 
        '''
        result = []
        
        file = open(filename, "r")
        for line in file.readlines():
            line = line.strip()
            if line != "":
                result.append(line.lower())
        file.close()
        
        return result
    
    def readWordFreq(self, filename):
        '''
        @return: dict<word, frequency = 1>
        '''
        keys = self.readWordList(filename)
        result = {}
        for key in keys:
            result[key] = 1
        return result
    
    def readResource(self, mechanizm, name):
        '''
        Read resource from: <path_to_resources>/mechanizm/name.txt
        @return: list of words
        @raise: IOError if not found
        '''
        path = os.path.join(Path.getResourceDir(), mechanizm, name + ".txt")
        if not os.path.exists(path):
            raise IOError
        
        return self.readWordList(path)
        
    def getMechanizmWordsFreq(self, mechanizm, name, path):
        '''@return: dict<name, list_of_words>'''
        paths = getMechanizmFilesDict(mechanizm)
        
        result = []
        for name, path in paths.items():
            result[name] = self.readWordList(path)
            
        return result
        
    def getMechanizmFilesDict(self, mechanizm, discriminator = "txt"):
        '''@return: dict<mechanizm_file_name, mechanizm_file_path>
        '''
        path = os.path.join(Path.getResourceDir(), mechanizm)
        if not os.path.exists(path):
            raise IOError
        
        result = {}
        for file in os.listdir(path):
            if re.match(".*" + discriminator, file, re.I):
                key = file[0:len(file) - len(discriminator) - 1]
                value = os.path.join(path, file)
                result[key] = value
        return result
        
    def getFilepathList(self, mechanizm, discriminator = "txt"):
        '''@return: list of path to files in specified directory given 
        by mechanizm name, filtered by discriminator
        '''
        return self.getMechanizmFilesDict(mechanizm, discriminator).values() 
    
    def getNameList(self, mechanizm, discriminator = "txt"):
        '''@return: list of path to files in specified directory given 
        by mechanizm name, filtered by discriminator
        '''
        return self.getMechanizmFilesDict(mechanizm, discriminator).keys() 
    
if __name__ == "__main__":
    rr = ResourceReader()
    print rr.getMechanizmFilesDict("subjects")
    print rr.getNameList("subjects")
    print rr.getFilepathList("subjects")